package cc.fs.zhihuapp.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Created by fostion on 2015/10/28.
 */
public class TimeUtil {

    public static String  formatVideoTime(int t){
        StringBuilder timeBuilder = new StringBuilder();
        if(t == 0){
            timeBuilder.append("00:00");
            return timeBuilder.toString();
        }

        int time = t / 1000;//获得秒
        int hour = 0;
        int minute = 0;
        int second = 0;
        hour = time / 3600;
        if (hour > 0) {
            timeBuilder.append("" + hour + ":");
            time = time - (3600 * hour);
        }

        minute = time / 60;
        if (minute > 0) {
            if (minute > 9) {
                timeBuilder.append("" + minute + ":");
            } else {
                timeBuilder.append("0" + minute + ":");
            }
            time = time - (60 * minute);
        } else {
            timeBuilder.append("00:");
        }

        second = time;
        if (second > 9) {
            timeBuilder.append("" + second + "");
        } else {
            timeBuilder.append("0" + second);
        }

        return timeBuilder.toString();
    }

    public static String formatTime(long millis, boolean text) {
        boolean negative = millis < 0;
        millis = Math.abs(millis);
        millis /= 1000;
        int sec = (int) (millis % 60);
        millis /= 60;
        int min = (int) (millis % 60);
        millis /= 60;
        int hours = (int) millis;

        String time;
        DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        format.applyPattern("00");

        DecimalFormat format2 = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        format2.applyPattern("000");
        if (text) {
            if (millis > 0)
                time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
            else
                time = (negative ? "-" : "") + min + ":" + format.format(sec);
        }else{
            time = "" + hours + min + sec;
        }

        return time;
    }

}
